package com.lemon.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.*;
import com.lemon.domain.dto.JianHuoDto;
import com.lemon.mapper.*;
import com.lemon.service.OutboundOrderDetailsService;
import com.lemon.utils.UserThreadLocal;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lemon.service.OutboundOrderPackageService;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class OutboundOrderPackageServiceImpl extends ServiceImpl<OutboundOrderPackageMapper, OutboundOrderPackage> implements OutboundOrderPackageService{
    private final OutboundOrderMapper outboundOrderMapper;
    private final OutboundOrderDetailsMapper outboundOrderDetailsMapper;
    private final OutboundOrderPackageMapper outboundOrderPackageMapper;
    private final OutboundOrderWeightsMapper outboundOrderWeightsMapper;
    private final SkuMapper skuMapper;

    @Override
    @Transactional
    public void packing(Long id, Integer bePackNum, Integer packNum) {
        OutboundOrderPackage aPackage = outboundOrderPackageMapper.selectById(id);
        aPackage.setPackageCount(aPackage.getPackageCount()+packNum);
        aPackage.setPackageTime(LocalDateTime.now());
        aPackage.setPackageEmpId(UserThreadLocal.get().getId());
        aPackage.setStatus(2);
        if (bePackNum == packNum){
            aPackage.setStatus(3);
            aPackage.setIsDel(2);
            OutboundOrderWeights outboundOrderWeights = new OutboundOrderWeights();
            outboundOrderWeights.setWeightsCode(aPackage.getPackageCode());
            outboundOrderWeights.setOutboundOrderDetailId(aPackage.getOutboundOrderDetailId());
            outboundOrderWeights.setStatus(1);
            Sku sku = skuMapper.selectById(aPackage.getSkuId());
            outboundOrderWeights.setWeightVal(sku.getProductWeight()*aPackage.getPackageCount());
            outboundOrderWeights.setWeightsUnit(sku.getUnit());
            outboundOrderWeights.setCreateTime(LocalDateTime.now());
            outboundOrderWeightsMapper.insert(outboundOrderWeights);
            OutboundOrder outboundOrder = outboundOrderMapper.selectOne(Wrappers.<OutboundOrder>lambdaQuery().eq(OutboundOrder::getInvoiceCode, aPackage.getPackageCode()));
            outboundOrder.setStatus(6);
            outboundOrderMapper.updateById(outboundOrder);
            outboundOrderPackageMapper.updateById(aPackage);
            return;
        }
        OutboundOrder outboundOrder = outboundOrderMapper.selectOne(Wrappers.<OutboundOrder>lambdaQuery().eq(OutboundOrder::getInvoiceCode, aPackage.getPackageCode()));
        outboundOrder.setStatus(5);
        outboundOrderMapper.updateById(outboundOrder);
        outboundOrderPackageMapper.updateById(aPackage);
    }


    @Override
    public PageResult getPage(Integer page, Integer pageSize, String invoiceCode,Integer status) {
        // 开启分页
        PageHelper.startPage(page, pageSize);
        List<OutboundOrderPackage> outboundOrderPackages = outboundOrderPackageMapper.selectList(Wrappers.<OutboundOrderPackage>lambdaQuery().like(ObjectUtil.isNotEmpty(invoiceCode),OutboundOrderPackage::getPackageCode, invoiceCode).eq(ObjectUtil.isNotEmpty(status),OutboundOrderPackage::getStatus, status).orderByAsc(OutboundOrderPackage::getStatus).orderByDesc(OutboundOrderPackage::getPackageTime));
        List<JianHuoDto> jianHuoDtos = new ArrayList<>();
        outboundOrderPackages.forEach(orderPackage -> {
            JianHuoDto jianHuoDto = new JianHuoDto();
            jianHuoDto.setId(orderPackage.getId());
            jianHuoDto.setInvoiceCode(orderPackage.getPackageCode());
            OutboundOrderDetails byId = outboundOrderDetailsMapper.selectById(orderPackage.getOutboundOrderDetailId());
            jianHuoDto.setCode(byId.getCode());
            Sku sku = skuMapper.selectById(byId.getSkuId());
            jianHuoDto.setTitle(sku.getTitle());
            jianHuoDto.setPreCount(byId.getPreCount());
            jianHuoDto.setProductWeight(sku.getProductWeight());
            jianHuoDto.setProductVolume(sku.getProductVolume());
            OutboundOrder outboundOrder = outboundOrderMapper.selectById(byId.getOutboundOrderId());
            jianHuoDto.setCustomerId(outboundOrder.getCustomerId());
            jianHuoDto.setCreateTime(outboundOrder.getCreateTime());
            jianHuoDto.setPackEmpId(orderPackage.getPackageEmpId());
            jianHuoDto.setPackTime(orderPackage.getPackageTime());
            jianHuoDto.setIsPackStatus(orderPackage.getStatus());
            jianHuoDto.setBePackNum(byId.getPreCount()-orderPackage.getPackageCount());
            jianHuoDto.setDonePackNum(orderPackage.getPackageCount());
            OutboundOrderWeights outboundOrderWeights = outboundOrderWeightsMapper.selectOne(Wrappers.<OutboundOrderWeights>lambdaQuery().eq(OutboundOrderWeights::getOutboundOrderDetailId, byId.getId()));
            if (ObjectUtil.isNotEmpty(outboundOrderWeights)){
                jianHuoDto.setIsChengZhong(outboundOrderWeights.getStatus());
            }else {
                jianHuoDto.setIsChengZhong(1);
            }
            jianHuoDtos.add(jianHuoDto);
        });
        // 获取分页信息
        Page<OutboundOrderPackage> pageResult = (Page<OutboundOrderPackage>) outboundOrderPackages;
        return new PageResult(pageResult.getTotal(), jianHuoDtos);
    }
}
